﻿using System.Collections.Generic;
using System.Linq;

namespace Sidvall.UI.Entities
{
    public class ChartItem : BusinessBase
    {
        #region Public Members

        public const double MaxExtraValue = 1.25;

        public string Name { get; set; }
        public string SeriesTitle { get; set; }
        public string VerticalAxisTitle { get; set; }
        public ChartItemData ChartItemData { get; set; }

        #region IsVisible

        public bool IsVisible
        {
            get
            {
                if (this.Values == null)
                    return false;
                return true;
            }
        }

        #endregion
        #region MaxValue

        public double? MaxValue
        {
            get
            {
                var value = GetValue(Sidvall.Data.AggregationOperator.Max, new ChartItemData[] { this.ChartItemData });
                return value * MaxExtraValue;
            }
        }

        #endregion
        #region TotalSum

        public double? TotalSum
        {
            get
            {
                if (this.Values == null)
                    return null;
                return this.Values.GetValue(Data.AggregationOperator.Sum);
            }
        }

        #endregion
        #region Values

        public ChartItemDataValueCollection Values
        {
            get
            {
                if (this.ChartItemData == null)
                    return null;
                return this.ChartItemData.Values;
            }
        }

        #endregion

        #region GetValue

        public static double? GetValue(Sidvall.Data.AggregationOperator aggregationOperator, IEnumerable<ChartItemData> items)
        {
            List<double> values;
            double? value;

            if (items == null)
                return null;
            values = new List<double>();
            foreach (var item in items)
            {
                if (item != null)
                {
                    value = item.GetValue(aggregationOperator);
                    if (value != null)
                        values.Add(value.Value);
                }
            }
            if (values.Count == 0)
                return null;
            switch (aggregationOperator)
            {
                case Sidvall.Data.AggregationOperator.Sum:
                    return values.Sum();
                case Sidvall.Data.AggregationOperator.Average:
                    return values.Average();
                case Sidvall.Data.AggregationOperator.Max:
                    return values.Max();
                case Sidvall.Data.AggregationOperator.Min:
                    return values.Min();
                default:
                    throw new System.ArgumentException("Invalid aggregation operator");
            }
        }

        #endregion

        #endregion
        #region Constructors

        public ChartItem()
        {
        }

        #endregion
    }
}